package com.seal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.seal.dao.BaseDaoImpl;
import com.seal.dao.IBusiImageDao;
import com.seal.dto.BusiImageDto;
import com.seal.model.BusiImage;

@Repository("busiImageDaoImpl")
public class BusiImageDaoImpl extends BaseDaoImpl implements IBusiImageDao {

	public void addBusiImages(List<BusiImage> list) throws Exception {
		for(BusiImage ca:list)
			this.save(ca);
	}

	public void updateBusiImage(BusiImage busiImage) throws Exception {
		this.update(busiImage);
	}

	public void deleteBusiImage(BusiImage busiImage) throws Exception {
		this.delete(busiImage);
	}
	
	public BusiImage getBusiImageById(Long id) throws Exception {
		return (BusiImage) this.select(BusiImage.class, id);
	}
	
	public List<BusiImage> getBusiImages(BusiImageDto dto) throws Exception {
		StringBuffer hql = new StringBuffer("from BusiImage t ");
		StringBuffer where = new StringBuffer(" where 1 = 1 ");

		if (null != dto.getBusiNo()) {
			where.append(" and busiNo = :busiNo ");
		}

		if (null != dto.getCompanyId()) {
			where.append(" and companyId = :companyId");
		}

		if (null != dto.getOperId()) {
			where.append(" and operId = :operId");
		}
		
		hql.append(where);
		hql.append(" order by orderNum asc");
		Query query = this.getSession().createQuery(hql.toString());
		query.setProperties(dto);
		return query.list();
	}

	public int getUseSpacesize(Long companyId) throws Exception {
		String sql = "select sum(filesize) from busi_image where company_id=" + companyId;
		List list = this.getSession().createSQLQuery(sql).list();
		if(list.get(0)==null)
			return 0;
		else
			return Integer.parseInt(list.get(0).toString());
	}
	
}
